package com.yy.QSManage.tool;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

/**
 * 对称加解/ASE
 */
public class EncryptTool {
    // 非对称加密私钥
    final static String RSA_PRIVATE_KEY = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAI7z3WXiyl+QC1xDbJWMlJRpx3pmJql8W4IYStmWP1YxeOYAzwsgmGU5blISyi1te6yYIQWlu258upUQlyFiSYzmnEu5ng2sR24JgmITpzrkYfAp8vQVUMvPjqoXzMe/p0vtUXZMnMPSltfucd8wGoAZPHV3pPg5FwXbFwilz8vJAgMBAAECgYABxBEVNTJFI7ADCeMVbMA40WuZL001Np/UdhuCzFYenBVKnxIvrwce6GQv3Jy1gRoAFLy2SrjlFjw0towG/PicWzkWvhZoj28xcsvudLlmRh8TWk+oOJ1fpcdS6CkScuLIoZW4vcyk/i+a1r11ZdFe5uyIpQBip9j52QumAfBf0QJBAL9ea5QkevnMbig+Mr02nxTIkoZm0YYxKVbE59AsjuBRB5l8ulqRtE2kxOl9O8G6DUlLyvFZu02JkT4KgUeIRbkCQQC/O2uEkYTljFNfNp5T0kvJOj2WMzJ3OCc77wF49Eb1EQay+rIGhclFNo8GWQhf4V7+wqCRhqD75BF/NRENab6RAkEAjpZMs+nmkz+GCIgniCItr++udWQg08rHkXZPfyXkIzFc5zkvJRKRdJTDf3GnV7ZH9gf6I5CHZcONq9tp7iGUSQJBAK809MxwOy/JgrVwqF0qBu+mIbkUzT73vtKlp1a3kK+F2vpuwF9DgmWcmCv7tLWMGmoVzeGnVKWUjwdainYclVECQQCgFDXNrkYlC2v6r3fNMmvr6a3Pnt8iWMBHIOmdWFW2RMk050QDY8L7uCTHpB1FzYyCorEUHMl+I5XCdvC8HyyI";
    // 非对称加密公钥，前端使用
    final static String RSA_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCO891l4spfkAtcQ2yVjJSUacd6ZiapfFuCGErZlj9WMXjmAM8LIJhlOW5SEsotbXusmCEFpbtufLqVEJchYkmM5pxLuZ4NrEduCYJiE6c65GHwKfL0FVDLz46qF8zHv6dL7VF2TJzD0pbX7nHfMBqAGTx1d6T4ORcF2xcIpc/LyQIDAQAB";

    /**
     * 非对称/私钥解密
     * @param content
     * @return
     */
    public static String rsaDecrypt(byte[] content) {
        String result = null;
        try {
            RSA rsa = new RSA(RSA_PRIVATE_KEY, RSA_PUBLIC_KEY);
            result = StrUtil.utf8Str(rsa.decrypt(content, KeyType.PrivateKey));
        }catch (Exception e){
            System.out.println("解密失败"+e.getMessage());
        }
        return result;
    }

    /**
     * 非对称/公钥加密
     * @param content
     * @return
     */
    public static byte[] rsaEncrypt(byte[] content) {
        byte[] result = null;
        try {
            RSA rsa = new RSA(RSA_PRIVATE_KEY, RSA_PUBLIC_KEY);
            result = rsa.encrypt(content, KeyType.PublicKey);
        }catch (Exception e){
            System.out.println("加密失败"+e.getMessage());
        }
        return result;
    }
}
